import 'dart:math';

import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class WrapperImage extends StatelessWidget {
  final String? url;
  final double? width;
  final double? height;
  final BoxFit? fit;
  final Widget?  placeholder;

  WrapperImage(
      {required this.url,
      required this.width,
      required this.height,
      this.fit: BoxFit.cover,
      this.placeholder
      });

  @override
  Widget build(BuildContext context) {
    return CachedNetworkImage(
      imageUrl: url!,
      width: width,
      height: height,
      placeholder: (_, __) =>
          placeHolder(width: width, height: height),
      errorWidget: (_, __, ___) => placeholder??error(width: width, height: height),
      fit: fit,
    );
  }

  Widget placeHolder({double? width, double? height}) {
    return SizedBox(
        width: width,
        height: height,
        child: CupertinoActivityIndicator(radius: min(10.0, width! / 3)));
  }

  Widget error({double? width, double? height, double? size}) {
    return SizedBox(
        width: width,
        height: height,
        child: Icon(
          Icons.error_outline,
          size: size,
        ));
  }
}
